<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="订单总金额" prop="totalAmount">
        <el-input v-model="queryParams.totalAmount" placeholder="请输入订单总金额" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
          <el-option v-for="dict in dict.type.order_status" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式" prop="paymentType">
        <el-select v-model="queryParams.paymentType" placeholder="请选择支付方式" clearable>
          <el-option v-for="dict in dict.type.pay_method" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="发货时间">
        <el-date-picker v-model="daterangeDeliveryTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="收货人姓名" prop="receiverName">
        <el-input v-model="queryParams.receiverName" placeholder="请输入收货人姓名" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="收货人电话" prop="receiverPhone">
        <el-input v-model="queryParams.receiverPhone" placeholder="请输入收货人电话" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="配送人姓名" prop="deliveryName">
        <el-input v-model="queryParams.deliveryName" placeholder="请输入配送人姓名" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['system:orders:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['system:orders:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:orders:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="订单编号" align="center" prop="orderNo"/>
      <!--      <el-table-column label="用户ID" align="center" prop="userId" />-->
      <!--      <el-table-column label="订单总金额" align="center" prop="totalAmount" />-->
      <el-table-column label="实付金额" align="center" prop="paymentAmount"/>
      <!--      <el-table-column label="打包费" align="center" prop="packAmount" />-->
      <!--      <el-table-column label="运费" align="center" prop="freightAmount" />-->
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <!--      <el-table-column label="支付方式" align="center" prop="paymentType">-->
      <!--        <template slot-scope="scope">-->
      <!--          <dict-tag :options="dict.type.pay_method" :value="scope.row.paymentType"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="支付时间" align="center" prop="paymentTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.paymentTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="发货时间" align="center" prop="deliveryTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.deliveryTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="收货时间" align="center" prop="receiveTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.receiveTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="订单备注" align="center" prop="remark"/>
      <el-table-column label="收货人姓名" align="center" prop="receiverName"/>
      <el-table-column label="收货人电话" align="center" prop="receiverPhone"/>
      <!--      <el-table-column label="地址簿id" align="center" prop="addressBookId" />-->
      <el-table-column label="收货人地址" align="center" prop="receiverAddress"/>
      <!--      <el-table-column label="取消订单原因" align="center" prop="cancelReason" />-->
      <!--      <el-table-column label="取消订单时间" align="center" prop="cancelTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.cancelTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="配送人id" align="center" prop="deliveryId"/>-->
      <!--      <el-table-column label="配送人姓名" align="center" prop="deliveryName"/>-->
      <!--      <el-table-column label="配送状态" align="center" prop="deliveryStatus">-->
      <!--        <template slot-scope="scope">-->
      <!--          <dict-tag :options="dict.type.delievry_status" :value="scope.row.deliveryStatus"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="预计送达时间" align="center" prop="estimatedDeliveryTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.estimatedDeliveryTime, '{y}-{m}-{d} {h}:{i}:{s}'') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="餐具数量" align="center" prop="tablewareNumber"/>-->
      <!--      <el-table-column label="餐具数量状态" align="center" prop="tablewareStatus">-->
      <!--        <template slot-scope="scope">-->
      <!--          <dict-tag :options="dict.type.tableware_status" :value="scope.row.tablewareStatus"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleOrderInfo(scope.row)">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" style="color: red" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:orders:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="订单详情" :visible.sync="openOrder" width="1600px" append-to-body>
      <h4>订单跟踪</h4>
      <div style="margin: 20px 0">
        <el-row>
          <el-steps :active="active" finish-status="success" align-center>
            <el-step v-for="item in form.takeoutLogList" :title="item.description"
                     :description="item.logTime"></el-step>
          </el-steps>
        </el-row>
      </div>
      <h4>订单明细</h4>
      <div>
        <el-form ref="form" :model="form" label-width="120px">
          <el-row>
            <el-col :span="8">
              <el-form-item label="订单号">
                <el-input v-model="form.orderNo" placeholder="暂无" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="收货人">
                <el-input v-model="form.receiverName" placeholder="暂无" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="手机号">
                <el-input v-model="form.receiverPhone" placeholder="暂无" readonly/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="订单状态">
                <el-select v-model="form.orderStatus" placeholder="暂无" >
                  <el-option v-for="dict in dict.type.order_status" :key="dict.value" :label="dict.label"
                             :value="parseInt(dict.value)"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="下单时间">
                <el-input v-model="form.paymentTime" placeholder="暂无" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="配送人">
                <el-input v-model="form.deliveryName" placeholder="暂无" readonly/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="收货时间">
                <el-input v-model="form.receiveTime" placeholder="暂无" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="配送状态">
                <el-select v-model="form.deliveryStatus" placeholder="暂无" disabled>
                  <el-option v-for="dict in dict.type.delivery_status" :key="dict.value" :label="dict.label"
                             :value="dict.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="预计送达时间">
                <el-input v-model="form.estimatedDeliveryTime" placeholder="暂无" readonly/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="地址">
                <el-input v-model="form.receiverAddress" placeholder="暂无" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="运费">
                <el-input v-model="form.freightAmount" placeholder="暂无" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="打包费">
                <el-input v-model="form.packAmount" placeholder="暂无" readonly/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="餐具数量状态">
                <el-select v-model="form.tablewareStatus" placeholder="按餐量提供" disabled>
                  <el-option v-for="dict in dict.type.tableware_status" :key="dict.value" :label="dict.label"
                             :value="parseInt(dict.value)"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="餐具数量">
                <el-input v-model="form.tablewareNumber" placeholder="暂无" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="备注">
                <el-input v-model="form.remark" type="textarea" placeholder="暂无" readonly></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <h4>商品明细</h4>
      <div>
        <el-table :data="form.detailsList" stripe style="width: 100%">
          <el-table-column prop="id" label="id"></el-table-column>
          <el-table-column label="图片" prop="dishImage">
            <template slot-scope="scope">
              <image-preview :src="scope.row.dishImage" :width="50" :height="50"/>
            </template>
          </el-table-column>
          <el-table-column prop="dishName" label="商品"></el-table-column>
          <el-table-column prop="dishFlavor" label="口味">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.t_flavor" :value="scope.row.dishFlavor"/>
            </template>
          </el-table-column>
          <el-table-column prop="quantity" label="数量"></el-table-column>
          <el-table-column prop="dishPrice" label="单价/元"></el-table-column>
          <el-table-column prop="totalPrice" label="总价/元"></el-table-column>
          <el-table-column prop="refundStatus" label="退款状态">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.refund_status" :value="scope.row.refundStatus"/>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOrders, getOrders, delOrders, addOrders, updateOrders} from "@/api/system/orders"

export default {
  name: "Orders",
  dicts: ['order_status', 'pay_method', 'delivery_status', 'tableware_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      ordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示订单详情弹出层
      openOrder: false,
      // 是否显示弹出层
      open: false,
      // 餐具数量状态时间范围
      daterangePaymentTime: [],
      // 餐具数量状态时间范围
      daterangeDeliveryTime: [],
      // 餐具数量状态时间范围
      daterangeReceiveTime: [],
      // 餐具数量状态时间范围
      daterangeEstimatedDeliveryTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        userId: null,
        totalAmount: null,
        packAmount: null,
        freightAmount: null,
        orderStatus: null,
        paymentType: null,
        paymentTime: null,
        deliveryTime: null,
        receiveTime: null,
        receiverName: null,
        receiverPhone: null,
        receiverAddress: null,
        cancelTime: null,
        deliveryId: null,
        deliveryName: null,
        deliveryStatus: null,
        estimatedDeliveryTime: null,
        tablewareStatus: null,
      },
      // 表单参数
      form: {},
      //日志激活状态
      active: 0,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangePaymentTime && '' != this.daterangePaymentTime) {
        this.queryParams.params["beginPaymentTime"] = this.daterangePaymentTime[0]
        this.queryParams.params["endPaymentTime"] = this.daterangePaymentTime[1]
      }
      if (null != this.daterangeDeliveryTime && '' != this.daterangeDeliveryTime) {
        this.queryParams.params["beginDeliveryTime"] = this.daterangeDeliveryTime[0]
        this.queryParams.params["endDeliveryTime"] = this.daterangeDeliveryTime[1]
      }
      if (null != this.daterangeReceiveTime && '' != this.daterangeReceiveTime) {
        this.queryParams.params["beginReceiveTime"] = this.daterangeReceiveTime[0]
        this.queryParams.params["endReceiveTime"] = this.daterangeReceiveTime[1]
      }
      if (null != this.daterangeEstimatedDeliveryTime && '' != this.daterangeEstimatedDeliveryTime) {
        this.queryParams.params["beginEstimatedDeliveryTime"] = this.daterangeEstimatedDeliveryTime[0]
        this.queryParams.params["endEstimatedDeliveryTime"] = this.daterangeEstimatedDeliveryTime[1]
      }
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: null,
        userId: null,
        totalAmount: null,
        paymentAmount: null,
        packAmount: null,
        freightAmount: null,
        orderStatus: null,
        paymentType: null,
        paymentTime: null,
        deliveryTime: null,
        receiveTime: null,
        remark: null,
        receiverName: null,
        receiverPhone: null,
        addressBookId: null,
        receiverAddress: null,
        cancelReason: null,
        cancelTime: null,
        deliveryId: null,
        deliveryName: null,
        deliveryStatus: null,
        estimatedDeliveryTime: null,
        tablewareNumber: null,
        tablewareStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangePaymentTime = []
      this.daterangeDeliveryTime = []
      this.daterangeReceiveTime = []
      this.daterangeEstimatedDeliveryTime = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 详情按钮操作 */
    handleOrderInfo(row) {
      this.reset()
      const id = row.id
      console.log(id)
      this.openOrder = true
      getOrders(id).then(res => {
        console.log(res.data)
        this.form = res.data
        this.active = this.form.takeoutLogList.length
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrders(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addOrders(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.orderNo || this.ids
      this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function () {
        return delOrders(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
